package cn.o.system.attachment.controller;

import cn.o.entity.base.Result;
import cn.o.entity.system.attachment.Attachment;
import cn.o.exceptions.CommonException;
import cn.o.exceptions.ExceptionEnum;
import cn.o.api.base.controller.BaseController;
import cn.o.system.attachment.service.AttachmentService;
import cn.o.api.base.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description:
 * @Author: Zhou Chenyu
 * @Date: 2018/10/25
 * @Version: V1.0
 * @Modified By:
 * @Modified Date:
 * @Why:
 */
@RestController
@RequestMapping("/attachment")
public class AttachmentController extends BaseController<Attachment> {

    @Autowired
    private AttachmentService attachmentService;

    @Override
    public BaseService getService() {
        return attachmentService;
    }

    @RequestMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result upload(@RequestPart(value = "file") MultipartFile file) {
        try {
            Attachment attachment = attachmentService.upload(file);
            return new Result(attachment);
        } catch (Exception e) {
            return new Result(new CommonException(ExceptionEnum.UPLOAD_FAIL));
        }
    }
}
